import inspect
from pydantic import conint

from common.base.base_api import BaseApi
from settings import settings
from services.relays_module.schemas.configs import RmConfig, RmTelemetry


class RelaysModuleApi(BaseApi):
    __slots__ = []

    def __init__(self):
        super(RelaysModuleApi, self).__init__(request_queue_uuid=settings.RELAYS_MODULE_REQUEST_QUEUE)

    async def set_configure(self, index: conint(ge=0, lt=settings.SERVICES_RELAYS_NUMBER), config: RmConfig
                      ) -> dict | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {'index': index, 'config': config}
        )

    async def get_configure(self, index: conint(ge=0, lt=settings.SERVICES_RELAYS_NUMBER)
                      ) -> RmConfig | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {'index': index},
            handle_result=lambda result: RmConfig(**result)
        )

    async def get_telemetry(self, index: conint(ge=0, lt=settings.SERVICES_RELAYS_NUMBER)
                      ) -> RmTelemetry | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {'index': index},
            handle_result=lambda result: RmTelemetry(**result)
        )
